package com.hadwinling.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hadwinling.blog.entity.Tag;
import com.hadwinling.blog.service.TagService;
import com.hadwinling.blog.util.FileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.invoke.VarHandle;
import java.util.List;

/**
 * @author HadwinLing
 * @version V1.0
 * @Package com.hadwinling.blog.controller
 * @date 2020/8/19 7:58
 */
@Controller
public class TagController {
    @Autowired
    private TagService tagService;
    @Autowired
    private FileUpload fileUpload;

    @Value("${admin.article.page.pageSize}")
    private int pageSize;
    @Value("${page.navigatePage}")
    private int navigatePage;
    /**
     * 跳转到查看标签页面
     * @return
     */
    @GetMapping("/admin/tag")
    public String toAdminTagPage(@RequestParam(name = "pageNum" ,defaultValue = "1")int pageNum , Model model){
        //分页
        PageHelper.startPage(pageNum,pageSize);
        List<Tag> tagList = tagService.selectAllByState(1);
        PageInfo<Tag> pageInfo = new PageInfo<>(tagList,navigatePage);
        //当前页码
        int currPage = pageInfo.getPageNum();
        //总记录
        Long total = pageInfo.getTotal();
        //总页数
        int totalPage = pageInfo.getPages();
        //所有页面
        int [] pages = pageInfo.getNavigatepageNums();
        //保存分页信息
        model.addAttribute("currPage",currPage);
        model.addAttribute("total",total);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("pages",pages);
        model.addAttribute("tagList",tagList);
        return "AdminCenter/tags";
    }

    /**
     * 执行删除操作
     * @param tagid
     * @return
     */
    @PostMapping("/admin/deleteTag")
    @ResponseBody
    public String deleteTag(@RequestParam(name = "tagid") int tagid){
        Tag tag = tagService.selectByPrimaryKey(tagid);
        tag.setState(0);
        tagService.updateByPrimaryKeySelective(tag);
        return "1";

    }

    /**
     * 执行置顶操作
     * @param tagid
     * @return
     */
    @PostMapping("/admin/addTopTag")
    @ResponseBody
    public String top(@RequestParam(name = "tagid") int tagid){
        //先判断置顶数是不是5个
        List<Tag> tagList = tagService.selectAllByTopAndState(1, 1);
        if (tagList.size()>5){
            //置顶数已经是5个了，不能在置顶
            return "0";
        }
        //置顶操作
        tagService.updateTopById(tagid,1);
        return "1";
    }
    /**
     * 执行取消置顶操作
     * @param tagid
     * @return
     */
    @PostMapping("/admin/noTopTag")
    @ResponseBody
    public String notop(@RequestParam(name = "tagid") int tagid){
        //先判断置顶数
        List<Tag> tagList = tagService.selectAllByTopAndState(1, 1);
        if (tagList.size()<=5){
            //置顶数已经是5个了，无法取消置顶
            return "0";
        }
        //取消置顶操作
        tagService.updateTopById(0,tagid);
        return "1";
    }



    /**
     * 跳转到修改标签页面
     * @param tagid
     * @param model
     * @return
     */
    @GetMapping("/admin/updateTag/{tagid}")
    public String toupdateTagPage(@PathVariable(name = "tagid") int tagid,Model model){
        Tag tag = tagService.selectByPrimaryKey(tagid);
        model.addAttribute("tag",tag);
        return "AdminCenter/tagsUpdate";
    }

    /**
     * 执行修改标签操作
     * @param tag
     * @param photo
     * @return
     * @throws IOException
     */
    @PostMapping("/admin/updateTag")
    public String updateTag(Tag tag,@RequestParam(name = "photo") MultipartFile photo) throws IOException {
        //获取文件全名
        String photoName = photo.getOriginalFilename();
        System.out.println("photoName:"+photoName);
        //首先判断是不是空的文件
        if (!photo.isEmpty()) {
            //对文文件的全名进行截取然后在后缀名进行删选。
            int begin = photo.getOriginalFilename().indexOf(".");
            int last = photo.getOriginalFilename().length();
            //获得文件后缀名
            String a = photo.getOriginalFilename().substring(begin, last);
            //判断是否为图片
            if ((".gif").equals(a)||(".GIF").equals(a)||
                    (".jpg").equals(a)||(".JPG").equals(a)||
                    (".PNG").equals(a)||(".png").equals(a)||
                    (".jpeg").equals(a)||(".JPEG").equals(a)){
                //这里是正确的图片格式
                String imgUrl = fileUpload.FileUploadToQiNiu(photo);
                tag.setTagphoto(imgUrl);
            }
        }
        tagService.updateByPrimaryKeySelective(tag);
//        System.out.println(tag);
        return "redirect:/admin/tag";
    }

    /**
     * 跳转到发布标签页面
     * @return
     */
    @GetMapping("/admin/addTag")
    public String toAdminAddTagPage(){
        return "AdminCenter/tagsInput";
    }


    /**
     * 执行发布标签操作
     * @param imgFileUp
     * @param tag
     * @return
     * @throws IOException
     */
    @PostMapping("/admin/addTag")
    public String addTag(MultipartFile imgFileUp, Tag tag) throws IOException {
        String imgUrl = fileUpload.FileUploadToQiNiu(imgFileUp);
        tag.setTagphoto(imgUrl);
        tag.setState(1);
        tag.setTagnum(0);
        tag.setTop(0);
        tagService.insertSelective(tag);
        return "redirect:/admin/tag";
    }




}
